/*
This is part of Screenbar, which is based in part on the Litestep
shell source code.

Copyright (C) 2008-2009 Jim Babcock
Copyright (C) 1997-2009 The LiteStep Development Team

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
#include "stdafx.h"

set<WindowData*> windowByDescription(string keyword, string arg)
{
	set<WindowData*> ret;
	
	if(keyword=="all")
	{
		if(arg=="")
			arg="all";
		set<VirtualDesktop*> desks = findDesks(arg);
		
		vector<WindowData*> windows;
		windowTracker->getWindows(NULL, windows);
		
		for(unsigned ii=0; ii<windows.size(); ii++)
		{
			if(desks.find(windows[ii]->desk) == desks.end())
				continue;
			ret.insert(windows[ii]);
		}
	}
	else if(keyword=="tasks")
	{
		if(arg=="")
			arg="all";
		set<VirtualDesktop*> desks = findDesks(arg);
		
		vector<WindowData*> windows;
		windowTracker->getWindows(NULL, windows);
		
		for(unsigned ii=0; ii<windows.size(); ii++)
		{
			if(desks.find(windows[ii]->desk) == desks.end())
				continue;
			if(!windows[ii]->getIsTask())
				continue;
			ret.insert(windows[ii]);
		}
	}
	else if(keyword==""
	     || keyword=="foreground"
	     || keyword=="focused")
	{
		ret.insert(windowTracker->getForegroundWindow());
	}
	else if(keyword=="clicked")
	{
		if(clickContext) {
			LayoutCacheNode *node = clickContext->getNode();
			if(node && node->context.getTask())
				ret.insert(node->context.getTask());
		}
	}
	else if(keyword=="dragged")
	{
		if(dragState.isDragging())
			ret.insert(dragState.getContext().getTask());
	}
	else if(keyword=="dropped")
	{
		if(dragState.isDropping()) {
			if(dragState.getDropContext().getTask())
				ret.insert(dragState.getDropContext().getTask());
		}
	}
	else if(keyword=="hovered")
	{
		Point mousePos = getCursorPos();
		ScreenbarPanel *panel = findPanel(mousePos.x, mousePos.y);
		mousePos -= panel->getTopLeft();
		LayoutLocation hovered = panel->elementAtPoint(mousePos.x, mousePos.y);
		ret.insert(hovered.context.getTask());
	}
	
	return ret;
}


bool parseTaskPosition(TaskData **positionRel, bool *positionAfter, string position)
{
	if(position=="hovered")
	{
		Point cursorPos = getCursorPos();
		ScreenbarPanel *panel = findPanel(cursorPos.x, cursorPos.y);
		if(!panel)
			return false;
		
		Layout *layout = panel->getLayout();
		
		Point layoutPos = cursorPos;
		layoutPos -= panel->getTopLeft();
		LayoutReference *ref = layout->elementAtPoint(layoutPos.x, layoutPos.y);
		LayoutCacheNode *node = ref->getNode();
		delete ref;
		
		// Find the deepest hovered element which includes a context task
		while(node->parent && node->parent->context.getTask())
			node = node->parent;
		
		if(!node)
			return false;
		if(!node->context.taskWindow)
			return false;
		
		// The new position is relevant to that element's context task, and
		// before or after it depending on whether the cursor is over the left
		// or right side of the task.
		*positionRel = node->context.getTask()->task;
		if(layoutPos.x >= node->boundingRect.left + node->boundingRect.width/2)
			*positionAfter = true;
		else
			*positionAfter = false;
	}
	else if(position=="first")
	{
		*positionRel = windowTracker->getFirstTask();
		*positionAfter = false;
	}
	else if(position=="last")
	{
		*positionRel = windowTracker->getLastTask();
		*positionAfter = true;
	}
	
	return true;
}
